#include "Note.h"

#include <cmath>

constexpr double PI = 3.14159265358979323846;

Note::Note(
	size_t tracks_num,
	size_t track_index,
	size_t pitch_range,
	size_t pitch_min,
	size_t pitch,
	size_t velocity,
	Size2 duration,
	Color color
) {
	double track_start_angle = 2 * PI * (double)track_index / (double)tracks_num;
	double track_angle = 2 * PI / (double)tracks_num;
	pitch_start_angle = track_start_angle + track_angle * (double)(pitch - pitch_min) / (double)pitch_range;
	pitch_end_angle = track_start_angle + track_angle * (double)(pitch - pitch_min + 1) / (double)pitch_range;
	note_duration = duration;
	note_color = _adjust_color(color, velocity);
}

Color Note::_adjust_color(Color raw_color, size_t velocity) {
	uint8_t velocity_uint8_t = (uint8_t)velocity;
	Color color = {
		.x = _adjust_color_value(raw_color.x, velocity_uint8_t),
		.y = _adjust_color_value(raw_color.y, velocity_uint8_t),
		.z = _adjust_color_value(raw_color.z, velocity_uint8_t)
	};
	return color;
}

uint8_t Note::_adjust_color_value(uint8_t raw_color_value, uint8_t velocity) {
	int color_value = (int)rint((double)raw_color_value * ((double)velocity / 127 + 0.5));
	if (color_value < 0)color_value = 0;
	else if (color_value > 255)color_value = 255;
	return (uint8_t)color_value;
}
